package org.jeecg.modules.ncmeapis.network.entities.entity.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.aspect.annotation.Dict;
import org.jeecg.modules.ncmeapis.network.entities.entity.NetworkPath;
import org.jeecg.modules.ncmeapis.network.entities.entity.dto.PathPermDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
public class PathWithPerm {

    public PathWithPerm(NetworkPath networkPath, PathPermDTO pathPermDTO) {
        BeanUtils.copyProperties(pathPermDTO, this);
        BeanUtils.copyProperties(networkPath, this);
    }

    @ApiModelProperty(value = "主键")
    private String id;

    @ApiModelProperty(value = "名称")
    private String name;

    @ApiModelProperty(value = "目录")
    private String path;

    @Dict(dicCode = "network_path_type")
    @ApiModelProperty(value = "目录类型(字典network_path_type)")
    private String type;

    @ApiModelProperty(value = "层级")
    private Integer depth;

    @ApiModelProperty(value = "创建人")
    private String createBy;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "创建日期")
    private Date createTime;

    @ApiModelProperty(value = "更新人")
    private String updateBy;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "更新日期")
    private Date updateTime;

    @ApiModelProperty(value = "所属部门")
    private String sysOrgCode;

    @Dict(dicCode = "yn")
    @ApiModelProperty(value = "是否有子节点(字典yn)")
    private String hasChild;

    private String perms;

    /**
     * 判断是否有权限
     */
    public boolean has(String... pathPermCodes) {
        if (pathPermCodes == null || pathPermCodes.length == 0) return false;
        for (String pathPermCode : pathPermCodes) {
            if (StringUtils.isEmpty(pathPermCode)) return false;
            if (StringUtils.isEmpty(this.perms)) return false;
            String[] split = this.perms.split(",");
            boolean contains = Arrays.stream(split).collect(Collectors.toList()).contains(pathPermCode);
            if (!contains) return false;
        }
        return true;
    }

    public boolean lacks(String... pathPermCodes) {
        return !has(pathPermCodes);
    }

}
